package com.leon.programPeral.ch2.ext3;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Ch2Ext3ChangeStringMoveClient
{
	public static void main(String[] args)
	{
		String str = WordsUtil.getRandomStr(1000000);
		List<Character> list = new ArrayList<>(str.length());
		for (int i = 0; i < str.length(); i++)
		{
			list.add(str.charAt(i));
		}
		int len = str.length();
		int i = len / 4;
		Set<Integer> set = new HashSet<>();
		int l = 0;
		long time = System.currentTimeMillis();
		while (set.size() != list.size())
		{

			oncemove(set, list, i, len, l);
			l++;
		}
		System.out.println(System.currentTimeMillis()-time);
		//		oncemove(list, i, len, 1);

//		printList(list);
	}

	private static void oncemove(Set<Integer> set, List<Character> list, int i, int len, int l)
	{

		char t = list.get(l);
		int k = 1;
		while ((i * k + l) % len != l)
		{
			int m = (i * (k - 1) + l) % len;
			int n = (i * k + l) % len;
			list.set(m, list.get(n));
			set.add(n);
			k++;
		}
		int o = (i * (k - 1) + l) % len;
		list.set(o, t);
		set.add(l);
	}

	private static void printList(List<Character> list)
	{
		StringBuilder sb = new StringBuilder();
		list.stream().forEach((a) -> sb.append(a));
		System.out.println(sb.toString());
	}

}
